﻿//公用函数

//获取地图数据
function GetMapData(url)
{
    getURL(url,UpdateMapData);
}

//更新地图数据
function UpdateMapData(data)
{
    if(data.success)
    {
        //RemoveChildNodes(mapObject); 
        RemoveMapData();    
       // mapObject.appendChild(parseXML(data.content,svgDocument));
        mapObject.insertBefore(parseXML(data.content,svgDocument),null);
    }
}

//移除指定节点下的所有子节点
function RemoveChildNodes(curNode)
{
    if(!curNode) return;
        
    while(curNode.hasChildNodes())
	{
		curNode.removeChild(curNode.childNodes.item(0));
	}
}

function RemoveMapData()
{
    var mapDataNode = svgDocument.getElementById("mapData");
    
    if(mapDataNode) mapObject.removeChild(mapDataNode);
}

//解析文本为svg节点，并将这个节点添加到指定父节点中
function AddChildNode(parentNode,childeNodeText)
{   
	return parentNode.insertBefore(parseXML(childeNodeText,svgDocument),null);
	//return parentNode.appendChild(parseXML(childeNodeText,svgDocument));
}

function GetCurrentViewBox()
{
    var viewBoxParamers = mapObject.getAttributeNS(null,"viewBox").split(" ");
    
    currentViewBoxX = parseFloat(viewBoxParamers[0]);
    currentViewBoxY = parseFloat(viewBoxParamers[1]);
    currentViewBoxWidth = parseFloat(viewBoxParamers[2]);
    currentViewBoxHeight = parseFloat(viewBoxParamers[3]);
    
     horizonScale = currentViewBoxWidth / containerWidth;
    verticalScale = currentViewBoxHeight / containerHeight;
    currentScale = 1 / horizonScale;
}

function SetViewBox(x, y, width, height)
{
    var viewBoxString = x + " " + y + " " + width + " " + height;
    mapObject.setAttributeNS(null,"viewBox",viewBoxString);
}

function SetMapFullExtent()
{
    SetViewBox(minLongitude,minLatitude,maxViewBoxWidth,maxViewBoxHeight);
}